<%--
    Document   : index
    Created on : Jun 20, 2012, 3:25:18 AM
    Author     : aspr0
--%>
<%@page import="is.model.dao.DepartmentDao"%>
<%@page import="is.interfaces.Dao"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.util.LinkedList"%>
<%@page import="is.web.GetParam"%>
<%@page import="is.web.table.DepartmentTable"%>
<%@page import="is.web.Message"%>
<%@page import="java.util.List"%>
<%@page import="is.model.bean.Department"%>
<%@page import="is.model.DataSource"%>
<%@page import="java.util.Iterator"%>
<%@page import="is.model.bean.Employee"%>


<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%
    request.setCharacterEncoding(is.Conf.ENCODING); // !!!

    String datatype = "";
    String content = "";

    String msgInfo = Message.genMsgInfo(request.getParameter(GetParam.INFO));
    String msgWarning = Message.genMsgWarning(request.getParameter(GetParam.WARNING));
    String msgError = Message.genMsgError(request.getParameter(GetParam.ERROR));

    try
    {
        List<Department> departments;
        DepartmentTable departmentTable = new DepartmentTable();
        departmentTable.setInitialBean(new Department());
        departmentTable.setOrderBy(request.getParameter(GetParam.ORDER_BY));

        datatype = departmentTable.getDatatypeName();

        DataSource.establishConnection();
        Dao<Department> departmentDao = new DepartmentDao();

        if (request.getMethod().equals("POST"))
        {
%>

<jsp:useBean id="searchDepartmentBean" class="is.model.bean.Department"/>
<jsp:setProperty name="searchDepartmentBean" property="*"/>

<%
            departmentDao.setInitialBean(searchDepartmentBean);
            departments = departmentDao.search(false);
            departmentTable.setInitialBean(searchDepartmentBean);
            departmentTable.setHasSearched(true);

            if (departments.size() < 1)
            {
                msgWarning = Message.genMsgWarning("3");
            }
            else
            {
                msgInfo = Message.genMsgInfo("3");
            }
        }
        else
        {
            departmentDao.setInitialBean(new Department());
            Integer departmentCount = departmentDao.count();

            if (departmentCount < 1)
            {
                msgWarning = Message.genMsgWarning("4");
                departments = new LinkedList<Department>();
            }
            else
            {
                departments = departmentDao.retrieveMultiple(0, departmentCount, departmentTable.getOrderBy());
            }
        }

        departmentTable.setBeans(departments);

        String table = departmentTable.genTable();
        DataSource.closeConnection();

        String msg = msgInfo + msgWarning + msgError;
        content = msg + table;
    }
    catch (SQLException e)
    {
        e.printStackTrace();
        content = Message.genMsgError("0") + content;
    }
    catch (Exception e)
    {
        if(e.getCause().getCause().getClass() == new NumberFormatException().getClass())
        {
            content = Message.genMsgWarning("0") + content;
        }
        else
        {
            e.printStackTrace();
            response.sendRedirect("?"+GetParam.ERROR+"=1");
        }
    }
%>

<jsp:include page="../template/index.jsp">
    <jsp:param name="datatype" value="<%=datatype%>"/>
    <jsp:param name="content" value="<%=content%>"/>
</jsp:include>
